import {
  getNewBookList,
  getHotBookList,
  getBookDetail,
  addedToCollection,
  removedFromCollection,
  updateBookInfo,
  getClassify,
  getBookClassify
} from "../Api/book";
import { getUserInfoBook, getOtherInfoBook } from "../Api/account";
const state = {
  currentBook: null,
  BookShelf:[],
  OtherBookShelf:[],
  newBookList: [],
  hotBookList: [],
  favorlist: [],
  classifyList:[],
  classifyBookList:[],
  
};
const mutations = {
  SET_CLASS_LIST(state,list){
		console.log(list);
		state.classifyList=list;
	},
  SET_CLASSIFY_LIST(state,list){
	  console.log(list);
	  state.classifyBookList=list;
  },
  SET_CURRENT_BOOK(state, book) {
    state.currentBook = book;
  },

  SET_NEW_BOOK_LIST(state, list) {
    state.newBookList = list;
  },
  SET_HOT_BOOK_LIST(state, list) {
    state.hotBookList = list;
  },
  SET_USERINFOBOOK_LIST(state,list){
	  state.BookShelf=list;
  },
  SET_OTHERINFOBOOK_LIST(state,list){
	  state.OtherBookShelf=list;
  },
  SET_FAVOR_LIST(state, list) {
    console.log(list)
      state.favorlist = list;
    },
  DELETE_COLLECT_ITEM(state,id){
   let index= state.favorlist.findIndex(item=>{
      return item.id==id
    })
    console.log(index)
    state.favorlist.splice(index,1)
  }
};

const actions = {
  async getOtherInfoBook({ commit }, id) {
    let result = await getOtherInfoBook(id);
    if (result.data.status == 200) {
      commit("SET_OTHERINFOBOOK_LIST", result.data.data);
    }
  },
  async getUserInfoBook({ commit }) {
    let result = await getUserInfoBook();
    console.log(result);
    if (result.data.status == 200) {
      commit("SET_USERINFOBOOK_LIST", result.data.data);
    }
  },
  async GetFavorList({ commit }) {
    let result = await favor();
	console.log(result)
    if (result.data.status == 200) {
      commit("SET_FAVOR_LIST", result.data.data);
    }
  },
  //修改书籍信息
  async updateBookInfo(context, data) {
    let result = await updateBookInfo(data);
    if (result.data.status == 200) {
      let ret = await context.dispatch("getBookDetail", data.id);
      if (ret == "success") return "success";
    } else {
      return "error";
    }
  },
  // 移除收藏
  async removeCollection(context, id) {
    // let id=context.state.favorlist[index].id
    console.log(id)
    let result = await removedFromCollection(id);
    console.log(result);
    if (result.data.status == 200) {
      context.commit('DELETE_COLLECT_ITEM',id)
      return "success";
    } else {
      return "error";
    }
  },
  // 添加到收藏
  async addedToCollection({ commit }, id) {
    let result = await addedToCollection(id);
    if (result.data.status == 200) {
      return "success";
    } else {
      return "error";
    }
  },
  //获取最新书籍列表
  async getNewBookList({ commit }, page) {
    let result = await getNewBookList(page);
    if (result.data.status == 200)
      commit("SET_NEW_BOOK_LIST", result.data.data);
  },
  //获取热门书籍列表
  async getHotBookList({ commit }) {
    let result = await getHotBookList();
    if (result.data.status == 200) {
      result.data.data.forEach((item, index) => (item.top = index + 1));
      commit("SET_HOT_BOOK_LIST", result.data.data);
    }
  },
  //查看书籍详情
  async getBookDetail({ commit }, id) {
    let result = await getBookDetail(id);
    if (result.data.status == 200) {
      console.log(result);
      if (result.data.data.imageList) {
        result.data.data.imageList = result.data.data.imageList.split("&||&");
      }
      commit("SET_CURRENT_BOOK", result.data.data);
      return "success";
    } else {
      return "error";
    }
  },
  //获取分类书籍
  async GetClass({ commit }) {
    let result = await getClassify();
    console.log(result);
    if (result.data.status == 200) commit("SET_CLASS_LIST", result.data.data);
  },
  //获取分类详情
  async GetClassifyDetail({ commit }, data) {
    // let test=new Object()
    // test.arr=[{a:1,b:2},{a:2,b:3},{a:1,b:55}]
    // console.log(test)
    // test.arr[0].a=9999
    // console.log(test)
    let result = await getBookClassify(data);

    if (result.data.status == 200) {
      result.data.data = result.data.data.filter((item) => {
        return item.status == 1;
      });
      commit("SET_CLASSIFY_LIST", result.data.data);
    } else {
      commit("SET_CLASSIFY_LIST", []);
    }
  },
};
const getters = {
  // 查看购物车中是否含有某本书

  newBookList(state) {
    return state.newBookList || [];
  },
  hotBookList(state) {
    return state.hotBookList || [];
  },
  hotBookListBySearch(state) {
    // 截取前10条
    return state.hotBookList.slice(0, 10);
  },
  //其他用户书籍
  getOtherBook(state) {
    return state.OtherBookShelf || {};
  },
  //用户书籍
  getUserBook(state) {
    return state.BookShelf || {};
  },
  getclassifyone(state) {
    // console.log(state)
    return state.classifyList;
  },
  getclassifytwo(state) {
    console.log(state);
    return state.classifyBookList;
  },
};
export default {
  state,
  mutations,
  actions,
  getters,
};
